<?php

class Job extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('job', 'job_model', 'job');
		$this->load->module_model('job', 'user_model', 'user');
		$lang = $this->load_language();
		//var_dump($lang);
		$this->load->module_language('job', 'general',$lang['name']);
		$this->load->module_language('job', 'sidebar',$lang['name']);
		$this->load->module_language('job', 'job',$lang['name']);
		//$this->load->module_language('job', 'job',);
		//$this->load->module_language('jo', 'general');
		//$this->load->module_language('user', 'registration');
	}

	// Public methods
	// confirm email by ajax method
	public function confirm()
	{
		$this->load->helper('email');
		$this->load->library('email');
		$email = $_POST['email'];
		$phone = $_POST['phone'];
		$fullname = $_POST['fullname'];
		$content = $_POST['content'];
		$f_email = 0; // 1 error
		$f_phone = 0; // 2 error, 3 e-error & p-error
		$f_fullname = 0; // 4 error, 5 e&f, 6 p&f, 7 e&p&f
		if (valid_email($email))
		{
			$f_emai = 0;
		}
		else
		{
			$f_emai = 1;
		}
		if($phone != '')
		{
			$f_phone = 0;
		}
		else
		{
			$f_phone = 1;
		}
		if($fullname != '')
		{
			$f_fullname = 0;
		}
		else
		{
			$f_fullname = 1;
		}
		$flag = $f_emai + $f_phone + $f_fullname;
		if($flag == 0)
		{
			//Start to send email
			$this->email->from($email, $fullname);
			$this->email->to('manhvt@ocd.vn'); // config
			
			$this->email->subject('Job board');
			$body = $phone. '<br />'. $content;
			$this->email->message($body);			
			$this->email->send();
			echo $flag;
		}
		else
		{
			echo $flag;
		}
			
	}
	
	public function post($year = NULL, $month = NULL, $day = NULL, $url_title = NULL)
	{
		$this->load_language();
		$this->load->library('ajax');
		$this->load->module_model('job', 'job_model', 'job');
			
		if ($data['job'] = $this->job->get_job_by_url($year, $month, $day, $url_title))
		{
			$data['job']['url'] = job_url($data['job']['job_url'], $data['job']['started_date']);
			$this->_template['page']	= 'job/single_job';
		}
		else
		{
			$this->_template['page']	= 'errors/404';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function index($page=null)
	{
		//echo "hello";
		
		$lang = $this->load_language();
		//var_dump($lang);
		$this->load->library('pagination');
		$this->load->module_language('job', 'general',$lang['name']);
		$this->load->module_language('job', 'sidebar',$lang['name']);
		$this->load->module_language('job', 'job',$lang['name']);
	
		$config['total_rows'] = $this->job->get_jobs_count($lang['id']);
		$config['per_page'] = $this->job->get_jobs_per_page();
		$this->pagination->initialize($config);
		
		$pages_count = ceil($config['total_rows'] / $config['per_page']);
		$page = ($page == 0) ? 1 : $page;
		$offset = $config['per_page'] * ($page - 1);
	
		if ($data['jobs'] = $this->job->get_jobs($config['per_page'], $offset,$lang['id']))
		{
			if ($page > $pages_count)
			{
				redirect('job', 'refresh');
			}
		
			$data['jobs_per_page'] = $this->job->get_jobs_per_page();
			$data['jobs_count'] = $this->job->get_jobs_count();
			$data['pages_count'] = $pages_count;
			$data['current_page'] = $page;
			$data['next_page'] = $page + 1;
			$data['previous_page'] = $page - 1;

			foreach ($data['jobs'] as $key => $job)
			{
				$data['jobs'][$key]['url'] = job_url($job['url_title'], $job['started_date']);
				$data['jobs'][$key]['display_name'] = $this->user->get_user_display_name($job['creator_id']);
			}

			$this->_template['page']	= 'job/index';
		}
		else
		{
			$this->_template['page']	= 'errors/no_posts';
		}
		
		$this->system_library->load($this->_template['page'], $data);
	}

	public function load_language()
	{

		$lang = $this->session->userdata('lang');
		
		//load language
		if($lang == false)
		{
			$language = $this->language->get_lang_id('vn');
			$lang = $language;
			$this->session->set_userdata('lang', $language);
		}
		//var_dump(strtolower($lang['name']));
		$this->load->module_language('job', 'general',$lang['name']);
		$this->load->module_language('job', 'sidebar',$lang['name']);
		return $lang;
	}
}

/* End of file user.php */
/* Location: ./application/modules/user/controllers/user.php */